﻿Imports System.Activities
Imports System.ComponentModel
Imports System.Activities.Presentation.PropertyEditing
Imports System.Activities.Presentation.Metadata
Imports System.Drawing

Public Class FileRenameActivity
    Inherits AsyncCodeActivity
    Implements IRegisterMetadata

    Public Property OriginalFilename As InArgument(Of String)
    Public Property NewFilename As InArgument(Of String)
    Public Property Result As OutArgument(Of Boolean)

    Private Delegate Function asyncFileRenameDelegate(ByVal original As String, ByVal target As String) As Boolean


    Public Sub New()
        Me.DisplayName = My.Resources.FileRenameDisplayName
    End Sub

    Protected Overrides Function BeginExecute(ByVal context As System.Activities.AsyncCodeActivityContext, ByVal callback As System.AsyncCallback, ByVal state As Object) As System.IAsyncResult
        Dim source = context.GetValue(Me.OriginalFilename)
        Dim dest = context.GetValue(Me.NewFilename)
        Dim asyncExecute = New asyncFileRenameDelegate(AddressOf FileRename)
        context.UserState = asyncExecute

        Return asyncExecute.BeginInvoke(source, dest, callback, state)
    End Function

    Protected Overrides Sub EndExecute(ByVal context As System.Activities.AsyncCodeActivityContext, ByVal result As System.IAsyncResult)
        Dim asyncExecute = TryCast(context.UserState, asyncFileRenameDelegate)
        Dim renameResult = asyncExecute.EndInvoke(result)

        context.SetValue(Me.Result, renameResult)
    End Sub

    Private Function FileRename(ByVal original As String, ByVal target As String) As Boolean
        Dim renameResult = False
        Try
            If Not System.IO.File.Exists(original) Then Exit Try '対象ファイルが存在しない場合はエラー
            If System.IO.File.Exists(target) Then Exit Try '新名称で既に存在はエラー

            My.Computer.FileSystem.RenameFile(original, target)
            renameResult = True
        Catch ex As Exception

        End Try
        Return renameResult
    End Function

    Public Sub Register() Implements System.Activities.Presentation.Metadata.IRegisterMetadata.Register
        Dim build As New AttributeTableBuilder

        'FileRename Activity
        build.AddCustomAttributes(GetType(FileRenameActivity),
                                              New ToolboxBitmapAttribute(GetType(FileRenameActivity), "FileRename.png"))
        build.AddCustomAttributes(GetType(FileRenameActivity), "OriginalFilename",
                                              New RequiredArgumentAttribute,
                                              New EditorAttribute(GetType(FilepathPropertyEditor), GetType(DialogPropertyValueEditor)),
                                              New CategoryAttribute(My.Resources.FileRenameCategory),
                                              New DisplayNameAttribute(My.Resources.FileRenameOriginalFileNameDisplay),
                                              New DescriptionAttribute(My.Resources.FileRenameOriginalFileNameDescription))
        build.AddCustomAttributes(GetType(FileRenameActivity), "NewFilename",
                                              New RequiredArgumentAttribute,
                                              New CategoryAttribute(My.Resources.FileRenameCategory),
                                              New DisplayNameAttribute(My.Resources.FileRenameNewFileNameDisplay),
                                              New DescriptionAttribute(My.Resources.FileRenameNewFileNameDescription))
        build.AddCustomAttributes(GetType(FileRenameActivity), "Result",
                                              New CategoryAttribute(My.Resources.FileRenameCategory),
                                              New DisplayNameAttribute(My.Resources.FileRenameResultDisplay),
                                              New DescriptionAttribute(My.Resources.FileRenameResultDescription))

        MetadataStore.AddAttributeTable(build.CreateTable)
    End Sub
End Class
